//
//  ImageTextTableViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/2/2.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "GKPhotoBrowser.h"

#import "StoreDetailNormalSectionHeader.h"

#import "PostDetailTextCell.h"
#import "DetailImageTableViewCell.h"
#import "PackageDetailRemindTableCell.h"

#import "ImageTextTableViewController.h"

@interface ImageTextTableViewController ()<UITableViewDelegate, UITableViewDataSource,GKPhotoBrowserDelegate>

@property (nonatomic, assign) NSInteger lastcontentOffset; //添加此属性的作用，根据差值，判断ScrollView是上滑还是下拉

@end

@implementation ImageTextTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {

    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (!self.canScroll) {
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.y<=0) {
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"tabNoti" object:nil];
    }

    CGFloat hight = scrollView.frame.size.height;
    CGFloat contentOffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentOffset;
    CGFloat offset = contentOffset - self.lastcontentOffset;
    self.lastcontentOffset = contentOffset;
    
    if (distanceFromBottom < hight) {
        if (offset > 0 && contentOffset > 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"packageShowQuanYiNoti" object:nil userInfo:nil];
        }
    }
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case DetailITSectionType_text:{
            return 1;
        } break;
        case DetailITSectionType_img:{
            return self.model.imgTextList.count;
        } break;
        case DetailITSectionType_reminder:{
            return 1;
        } break;
        default:{
            return 0;
        } break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case DetailITSectionType_text:{
            
            if (self.model.sellerGoodsModel.goodsDetailText.length == 0) {
                return 0;
            } else {
                return [Utilities getStringHeightWithText:self.model.sellerGoodsModel.goodsDetailText font:[UIFont systemFontOfSize:15] viewWidth:(TTScreenWidth - 28)] + 10;;
            }
        } break;
        case DetailITSectionType_img:{
            
//            if (self.model.imgTextList[indexPath.row].itemHeight > 0) {
                return self.model.imgTextList[indexPath.row].itemHeight;
//            } else {
//                return TTScreenWidth;
//            }

        } break;
        case DetailITSectionType_reminder:{
            return 130;
        } break;
        default:{
            return 0;
        } break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    switch (section) {
        case DetailITSectionType_text:{} break;
        case DetailITSectionType_img:{} break;
        case DetailITSectionType_reminder:{

            StoreDetailNormalSectionHeader *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreDetailNormalSectionHeader"];
            headView.contentView.backgroundColor = UIColor.whiteColor;
            headView.sectionTitle.text = @"温馨提示";
            headView.sectionContent.text = @"";
            return headView;

        } break;
        default:{
            return 0;
        } break;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    switch (section) {
        case DetailITSectionType_text:{return CGFLOAT_MIN;} break;
        case DetailITSectionType_img:{return CGFLOAT_MIN;}{} break;
        case DetailITSectionType_reminder:{
            return 50;
        } break;
        default:break;
    }
    return CGFLOAT_MIN;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case DetailITSectionType_text:{

            PostDetailTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostDetailTextCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.contentLab.text = self.model.sellerGoodsModel.goodsDetailText;
            return cell;

        } break;
        case DetailITSectionType_img:{

            DetailImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageTableViewCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            
            WEAKSELF
//            NSString *resizeUrl = [TT_OSSManager reformWebPUrl:self.model.imgTextList[indexPath.row].url];
//            NSString *resizeUrl = self.model.imgTextList[indexPath.row].url;
            NSString *resizeUrl = [TT_OSSManager reSourceUrl:self.model.imgTextList[indexPath.row].url type:BA_resizeType_webP];

            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:resizeUrl]  placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                if (image) {
                    weakSelf.model.imgTextList[indexPath.row].itemHeight = (image.size.height*(kScreenWidth)/image.size.width);
                } else {
                    weakSelf.model.imgTextList[indexPath.row].itemHeight = CGFLOAT_MIN;
                }

                [weakSelf.tableview reloadRow:indexPath.row inSection:indexPath.section withRowAnimation:UITableViewRowAnimationNone];
//                [weakSelf.tableview reloadData];
            }];
            
            return cell;

        } break;
        case DetailITSectionType_reminder:{

            PackageDetailRemindTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PackageDetailRemindTableCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            return cell;

        } break;
        default:{} break;
    }
    
    DetailImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageTableViewCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == DetailITSectionType_img) {
        if (self.model.images.count > 0) {
            [self showImageWithArray:self.model.images index:indexPath.row];
        }
    }
}

- (void)showImageWithArray:(NSArray *)imageUrls index:(NSInteger)index {
    NSMutableArray *photos = [NSMutableArray new];
    
    [imageUrls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:obj];
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
    
    browser.delegate  = self;
    [browser showFromVC:self];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"PostDetailTextCell" bundle:nil] forCellReuseIdentifier:@"PostDetailTextCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"PackageDetailRemindTableCell" bundle:nil] forCellReuseIdentifier:@"PackageDetailRemindTableCellID"];
        
        [_tableview registerClass:[DetailImageTableViewCell class] forCellReuseIdentifier:@"DetailImageTableViewCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailNormalSectionHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"StoreDetailNormalSectionHeader"];
        
    }
    return _tableview;
}

@end
